<template>
  <div class="container">
    <div class="sbgl-nav breadcrumb">
      <img src="static/img/shu.png" alt="">
      <span>资源管理</span>
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span>设备组管理</span>
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span id="labelNav">添加设备组</span>
    </div>
    <form class="form-horizontal" id="formAdd" v-if="isAdd" v-on:submit.prevent="submit">
      <div class="form-group">
          <label for="inputtext" class="col-sm-2 control-label col-sm-offset-3">设备组名称：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext" name="groupname" value="" v-model="groupModel.groupname">
          </div>
      </div>
      <div class="form-group">
          <label class="col-sm-2 control-label col-sm-offset-3">上级设备组名称：</label>
          <div class="col-sm-4">  
            <select class="form-control luxiang_xz" name="parentgroupid" v-model="groupModel.parentgroupid">
                <option value="" selected="selected">无</option>
                <option v-for="item in deviceList" v-bind:value="item.groupid">{{item.groupname}}</option>
            </select>
          </div>
      </div>
      <p class="lx_btn">
        <button type="submit" class="btn btn-default btn-lg">保存</button>
        <button type="button" onclick="window.history.go(-1)" class="btn btn-default btn-lg">返回</button>
      </p>
    </form>
    <form class="form-horizontal" id="formEdit" v-else v-on:submit.prevent="submit">
      <div class="form-group">
          <label for="inputtext1" class="col-sm-2 control-label col-sm-offset-3">设备组ID：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext1" readonly="readonly" name="groupid" v-model="groupModel.groupid">
          </div>
      </div>
      <div class="form-group">
          <label for="inputtext" class="col-sm-2 control-label col-sm-offset-3">设备组名称：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="inputtext" name="groupname" v-model="groupModel.groupname">
          </div>
      </div>
      <div class="form-group">
          <label class="col-sm-2 control-label col-sm-offset-3">上级设备组名称：</label>
           <div class="col-sm-4"> 
              <select class="form-control luxiang_xz" name="parentgroupid" v-model="groupModel.parentgroupid">
              <option value="">无</option>
              <option v-for="item in deviceList" v-bind:value="item.groupid">{{item.groupname}}</option>
          </select>
          </div>
      </div>
      <p class="lx_btn">
        <button type="submit" class="btn btn-default btn-lg">保存</button>
        <button type="button" onclick="window.history.go(-1)" class="btn btn-default btn-lg">返回</button>
      </p>
    </form>
  </div>
</template>

<script>
  export default {
    name: 'AddGroup',
    data: function () {
      return {
        groupId: this.$route.params.groupId,
        remoteip: '',
        method: this.$route.params.method || 'add',
        port: '',
        parentgroupid: '',
        deviceList: [],
        groupModel: {
          groupid: '',
          groupname: '',
          parentgroupid: ''
        }
      }
    },
    created: function () {
      this.isAdd = this.method === 'add'
      if (!this.isAdd) {
        this.editInit()
      }
      this.deviceInit()
    },
    methods: {
      submit: function () {
        var data = this.groupModel
        data.method = 'AddGroup'
        if (!this.isAdd) {
          data.method = 'UpdateGroup'
        }
        console.log(data)
        this.$http.post('/api/hedacmdreq', data).then(response => {
          var body = response.data.HedaACK.Body
          window.duang.msg(true, body.retMsg)
        })
        console.log('submit')
      },
      editInit: function () {
        this.$http.post('/api/hedacmdreq', {method: 'GroupSearch', group: this.groupId}).then(response => {
          var body = response.data.HedaACK.Body
          if (body && body.grouplist && body.grouplist.length > 0) {
            this.groupModel = body.grouplist[0]
          }
        })
      },
      deviceInit: function () {
        this.$http.post('/api/hedacmdreq', {method: 'GroupSearch'}).then(response => {
          console.log(response)
          var body = response.data.HedaACK.Body
          this.deviceList = body.grouplist || []
        })
      }
    }
  }
</script>
